package com.aopcinemas.cinema.ui;

import jade.core.Agent;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.aopcinemas.cinema.behaviour.CinemaRegistrationBehaviour;
import com.aopcinemas.cinema.model.Cinema;

public class CinemaRegistrationDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private Agent agent;

    public CinemaRegistrationDialog(JFrame owner, Agent agent) {
        super(owner, true);
        this.agent = agent;
        setTitle("Register cinema");
        setSize(300, 150);
        setResizable(false);
        getContentPane().add(new RegistrationPanel());
    }

    private class RegistrationPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private JTextField name = new JTextField(15);
        private JTextField organization = new JTextField(15);
        private JButton register = new JButton("Register");
        private JButton unregister = new JButton("Unregister");

        public RegistrationPanel() {

            GridBagLayout layout = new GridBagLayout();
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.insets = new Insets(3, 3, 3, 3);

            setLayout(layout);

            register.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    Cinema cinema = new Cinema();
                    cinema.setName(name.getText());
                    cinema.setOrganization(organization.getText());
                    agent.addBehaviour(new CinemaRegistrationBehaviour(agent, cinema));
                    register.setEnabled(false);
                    unregister.setEnabled(true);
                }
            });
            unregister.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    agent.addBehaviour(new CinemaRegistrationBehaviour(agent, null, CinemaRegistrationBehaviour.UNREGISTER));
                    register.setEnabled(true);
                    unregister.setEnabled(false);
                }
            });
            unregister.setEnabled(false);

            JLabel label = new JLabel("name: ");
            constraints.anchor = GridBagConstraints.EAST;
            layout.setConstraints(label, constraints);
            add(label);

            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.anchor = GridBagConstraints.WEST;
            layout.setConstraints(name, constraints);
            add(name);

            label = new JLabel("organization: ");
            constraints.gridwidth = 1;
            constraints.anchor = GridBagConstraints.EAST;
            layout.setConstraints(label, constraints);
            add(label);

            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.anchor = GridBagConstraints.WEST;
            layout.setConstraints(organization, constraints);
            add(organization);

            JPanel buttonPanel = new JPanel();
            buttonPanel.add(register);
            buttonPanel.add(unregister);

            constraints.gridwidth = 2;
            constraints.anchor = GridBagConstraints.EAST;
            layout.setConstraints(buttonPanel, constraints);
            add(buttonPanel);
        }

    }

}
